// import {createRouter, createWebHistory} from 'vue-router'
//
// const routes = createRouter({
//     history: createWebHistory(),
//     routes: [
//         {name: 'home', path: '/', component: () => import('../views/HelloWorld.vue')},
//         {name: 'login', path: '/login', component: () => import('../views/login.vue')},
//         {name: 'getCrouse', path: '/getCrouse', component: () => import('../views/getCrouse.vue')},
//     ]
// })
// export default routes


import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router';

// 导入需要进行身份验证的页面组件
import HelloWorld from '../views/HelloWorld.vue';
import login from '../views/login.vue';
import getCrouse from '../views/getCrouse.vue';
import getPlan from '../views/getPlan.vue';
import admin from '../views/admin.vue';
import home from '../views/adminHome.vue';
import * as CryptoJS from 'crypto-js';
import {ElMessage} from "element-plus";
// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes: [
        {name: 'home', path: '/', component: getCrouse},
        {name: 'login', path: '/login', component: login},
        {name: 'admin', path: '/admin', component: admin},
        {name: 'getCrouse', path: '/getCrouse', component: getCrouse},
        {name: 'getPlan', path: '/getPlan', component: getPlan},
        {name: 'home', path: '/adminhome', component: home},
    ]
});

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    // const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    const isCookieValid = validateCookie(); // 调用验证 cookie 的函数
    // 如果目标为登录页面，则直接放行导航
    if (to.path === '/login' || to.path === '/admin') {
        next(); // 如果目标路径已经是登录路径，则直接放行导航
    } else if (isCookieValid) {
        // cookie 有效，或者不需要验证，继续导航到目标页面
        next();
    } else {
        // ElMessage.error('学号或密码错误')
        // 如果需要验证且 cookie 无效，重定向到登录页面
        next('/login');
    }
});


function decryptDES(ciphertext: string, key: string, iv: string): string {
    const decrypted = CryptoJS.DES.decrypt({
        ciphertext: CryptoJS.enc.Hex.parse(ciphertext),
    }, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Hex.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });

    return decrypted.toString(CryptoJS.enc.Utf8);
}


// 验证 cookie 的函数
function validateCookie() {
    const cookies = document.cookie.split(';'); // 将所有 Cookie 拆分成数组
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim().split('=');
        console.log(cookie);
        if (cookie[0] == 'expiration_time') {
            const ciphertext = cookie[1];
            const key = '88888888';
            const iv = '0000000000000000';
            const plaintext = decryptDES(ciphertext, key, iv);
            // 将plaintext转换为数字
            const expiration_time = parseInt(plaintext);
            // 获取当前时间戳
            const timestamp = Date.parse(new Date().toString()) / 1000;
            if (expiration_time > timestamp) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

export default router;
